﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Threading.Tasks;
using EasyClassWEBAPI.Models;
using EasyClassWEBAPI.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EasyClassWEBAPI.Controllers
{
    [Route("User")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private EasyClassContext db;
        public UserController(EasyClassContext context)
        {
            this.db = context;
        }
        /// <summary>
        /// 登录账号 Get:User/Login 
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        [HttpGet("Login")]
        public async Task<IActionResult> Login(string UserAccount,string Password)
        {
            try
            {
                User cur = await db.Users.FirstOrDefaultAsync(o => o.UserAccount == UserAccount);
                //没有找到此账号(404 Not Found)
                if (cur == null) return NotFound("Not found this account");
                //MD5加密校验
                Password = Utils.MD5.GetMD5Hash(Password);
                if (cur.Password == Password)
                {
                    /*登录成功之后需要返回Token*/
                    TokenInfo info = new TokenInfo(cur.UserAccount, cur.Password);
                    cur.Token = TokenTools.TokenEncoder(info);
                    await db.SaveChangesAsync();
                    return Ok(cur.Token);
                }
                else
                    return BadRequest("Password Match Failed!");
                
            }
            catch
            {
                return BadRequest("Login Failed, Please check your information.");
            }
        }
        /// <summary>
        /// 注册账号 Post:User/Register
        /// </summary>
        /// <param name="NewUser"></param>
        /// <returns></returns>
        [HttpPost("Register")]
        public async Task<IActionResult> Register(User NewUser)
        {
            try
            {
                User cur = await db.Users.FirstOrDefaultAsync(o => o.UserAccount == NewUser.UserAccount);
                if (cur != null) return BadRequest("User has existed!");
                NewUser.Password = Utils.MD5.GetMD5Hash(NewUser.Password);
                db.Users.Add(NewUser);
                await db.SaveChangesAsync();
                return Ok("Register Successfully");
            }catch
            {
                return BadRequest("Register Failed");
            }
        }
        /// <summary>
        /// 删除账号 Delete:User/Delete
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<IActionResult> Delete(string UserAccount,string Password)
        {
            try
            {
                var user = await db.Users.FirstOrDefaultAsync(o => o.UserAccount == UserAccount);
                Password = Utils.MD5.GetMD5Hash(Password);
                if (user == null) return NotFound("查无此用户");
                if (user.Password != Password) return BadRequest("密码错误");
                db.Users.Remove(user);
                await db.SaveChangesAsync();
                return Ok("Delete Successfully");
            }catch
            {
                return BadRequest("删除用户权限不足");
            }
        }
    }
}
